<?php
/**
 * Created by PhpStorm.
 * User: 84071
 * Date: 2018-10-04
 * Time: 11:57
 */
namespace app\api\controller;

use app\common\controller\Apibase;
use app\common\model\Smscode;
use app\common\model\User as cxModel;
use think\Loader;

class User extends Apibase{
    /**
     * 本方法仅接收post值
     * 手机号为必填项 默认密码为手机号后6位
     * @return \think\response\Json
     */
    public function reg(){
        $getlist['code'] = '0';
        if(!request()->isPost()){
            $getlist['msg'] = '访问错误！';
            return json($getlist);
        }
        $getdata = input();
        $getdata = datatrim($getdata);
        //  如无密码，则取手机后6位做为密码
        if(empty($getdata['upassword'])){
            $getdata['upassword'] = $getdata['rupassword'] = substr($getdata['uphone'],-6);
        }
        //  验证用户提交的数据
        $validate = Loader::validate('User');
        if (!$validate->scene('reg')->check($getdata)) {
            $getlist['msg'] = $validate->getError();
            return json($getlist);
        }
        //  检测网站是否开启手机注册验证码，若开启则进行验证，验证手机验证码是否正确
        if($this->webdb['smsyzm'] == '1'){
            if(empty($getdata['uncode'])){
                $getlist['msg'] = '请输入验证码';
                return json($getlist);
            }
            $smscodemodel = new Smscode();
            if(!$smscodemodel->codeSms($getdata['uphone'],$getdata['uncode'])){
                $getlist['msg'] = '验证码错误';
                return json($getlist);
            }
            $getdata['uphone_yz'] = '1';
        }
        $cxmodel = new cxModel();
        if($cxmodel->api_reg($getdata)){
            webCacheRm('lishicode'.$getdata['uphone']);
            $getlist['code'] = '1';
            $getlist['msg'] = '注册成功';
            return json($getlist);
        }
        $getlist['msg'] = '注册失败';
        return json($getlist);
    }

    /**
     * 本方法仅接收post值 接收唯一手机号 及 验证码
     * @return \think\response\Json
     */
    public function repwd(){
        $getlist['code'] = '0';
        if(!request()->isPost()){
            $getlist['msg'] = '访问错误！';
            return json($getlist);
        }
        $getdata = input();
        $getdata = datatrim($getdata);
        //  如无重复密码，则复制密码
        if(empty($getdata['rupassword'])){
            $getdata['rupassword'] = $getdata['upassword'];
        }
        //  验证用户提交的数据
        $validate = Loader::validate('User');
        if (!$validate->scene('repwd')->check($getdata)) {
            $getlist['msg'] = $validate->getError();
            return json($getlist);
        }
        //  查询用户是否存在
        $cxmodel = new cxModel();
        $olddata = $cxmodel->where('username',$getdata['uphone'])->find();
        if(empty($olddata)){
            $getlist['msg'] = '用户不存在';
            return json($getlist);
        }
        $olddata = $olddata->toArray();
        //  检测验证码是否正确
        $smscodemodel = new Smscode();
        if(!$smscodemodel->codeSms($getdata['uphone'],$getdata['uncode'])){
            $getlist['msg'] = '验证码错误';
            return json($getlist);
        }
        //  修改密码
        if($cxmodel->repwd($olddata)){
            webCacheRm('lishicode'.$getdata['uphone']);
            $getlist['code'] = '1';
            $getlist['msg'] = '密码修改成功';
            return json($getlist);
        }
        $getlist['msg'] = '密码修改失败';
        return json($getlist);
    }
    //登录
    public function login(){
        $getlist['code'] = '0';
        if(!request()->isPost()){
            $getlist['msg'] = '访问错误！';
            return json($getlist);
        }
        $getdata = input();
        $getdata = datatrim($getdata);
        //  验证用户提交的数据
        $validate = Loader::validate('User');
        if (!$validate->scene('login')->check($getdata)) {
            $getlist['msg'] = $validate->getError();
            return json($getlist);
        }
        $cxmodel = new cxModel();
        $old = $cxmodel->where('username',$getdata['username'])->count();
        if($old == '0'){
            $getlist['msg'] = '用户不存在';
            return json($getlist);
        }
        $user = $cxmodel->api_login($getdata);
        if($user == false){
            $getlist['msg'] = '用户名或密码错误';
            return json($getlist);
        }else{
            $getlist['code'] = '1';
            $getlist['msg'] = '登录成功';
            $getlist['data'] = $user;
            return json($getlist);
        }
    }
    //  用户收藏
    public function Ucollection(){
        $res['code'] = '0';
        $getdata = input();
        $getdata = datatrim($getdata);
        if(empty($getdata['uid']) || empty($getdata['aid'])){
            $res['msg'] = '非法访问';
            return json($res);
        }
        $cxmodel = new cxModel();
        if($cxmodel->shoucang($getdata)){
            $res['code'] = '1';
            $res['msg'] = '操作成功';
        }else{
            $res['msg'] = '操作失败';
        }
        return json($res);
    }
    // 查询用户
    public function get_uid(){
        if(request()->isPost()){
            $cxmodel = new cxModel();
            $data = input('post.');
            $data = datatrim($data);
            if(!empty($data['m'])){
                $map['group_id'] = array('in',$data['m']);
            }
            $map['username|uniname|uemail|uname|uphone'] = array('like',"%".$data['k']."%");
            $aids = $cxmodel->where($map)->where('status','1')->order('uid desc')->select();
            $getaid = array();
            if(!empty($aids) && isset($aids)){
                foreach ($aids as $v){
                    $getaid[] = $v->toArray();
                }
            }
            return json($getaid);
        }
    }
}